$(function() {
	var h = $(window).height() ;
	//自适应容器大小
	/* $('.right-iframe').load(function(){
		$('.right-iframe').height($($('.right-iframe')[0].contentWindow.document.body).height()+10);
	}); */
	$(window).resize(function(){
		$('.right-iframe').height($(window).height()-80);
	});
	 $(".table-container").resize(
			function() {
				
				console.info("width: "+$(".table-container").width());
				$("#tree").setGridWidth($(".table-container").width()-1);
			}); 
	$(".widget-toolbar .btn-add").click(function() {
		var grid = $("#tree");
	    var rowKey = grid.jqGrid('getGridParam',"selrow");
	    if (rowKey){
	    	$(".right-iframe").attr("src","/subject/form?parent.id="+rowKey)
	    }else{
	    	$(".right-iframe").attr("src","/subject/form")
	    }
		
	});
	$(".widget-toolbar .btn-update").click(function() {
		var grid = $("#tree");
	    var rowKey = grid.jqGrid('getGridParam',"selrow");
	    if (!rowKey){
	    	window.top.layer.msg("请选择一条记录");
	    	return;
	    }
	        
		$(".right-iframe").attr("src","/subject/form?subjectId="+rowKey);
	});
	
	$(".widget-toolbar .btn-delete").click(function() {
		var grid = $("#tree");
	    var rowKey = grid.jqGrid('getGridParam',"selrow");
	    if (!rowKey){
	    	window.top.layer.msg("请选择一条记录");
	    	return;
	    }
	    
	    $.ajax({
			url : "/subject/deleteSubject?subjectId="+rowKey,
			type:"get",
			success : function(resp) {
				window.top.layer.msg("删除成功");
				$("#tree").delTreeNode(rowKey);
			}
		});
	});
	jQuery('#tree').jqGrid({
		cmTemplate:{
			sortable:false,
		},
		viewsortcols: [true,'vertical',true],
		autowidth:true,
		"hoverrows" : false,
		"viewrecords" : false,
		"gridview":false,
		"url" : "/subject/listSubject",
		/* "editurl" : "clientArray", */
		"ExpandColumn" : "name",
		"sortname":"sort",
		sortorder: 'asc',
		"scrollrows":true,
		"treeGrid" : true,
		"treedatatype" : "json",
		"treeGridModel" : "adjacency",
		loadonce:true,
		ExpandColClick:true,
		"rowNum" : 1000,
		"treeReader" : {
			"parent_id_field" : "parent_id",
			"leaf_field" : "is_leaf",
			"level_field":"level",
		},
		"ExpandColClick" : true,
		"datatype" : "json",
		"colModel" : [ {
			"name" : "id",
			"key" : true,
			"hidden" : true
		}, {
			"name" : "name",
			"label" : "栏目名称",
		}, {
			"name" : "dept_name",
			"label" : "所属部门",
		}, {
			"name" : "code",
			"label" : "栏目编码",
		}, {
			"name" : "image",
			"label" : "图片",
		}, {
			"name" : "sort",
			"sorttype" : "integer",
			"label" : "排序",
			sortable:true,
		}, {
			"name" : "is_comment",
			"label" : "是否评论",
			formatter: 'checkbox',
		}, {
			"name" : "is_audit",
			"label" : "是否审核",
			formatter: 'checkbox',
		}, {
			"name" : "status",
			"label" : "是否可用",
			formatter: 'checkbox',
		}, {
			"name" : "parent_id",
			"hidden" : true
		}, {
			"name" : "操作",
			"hidden" : true,
			formatter : function(cellvalue) {
				return "<a>修改</a><a>删除</a><a>添加下级栏目</a>";
			}
		}
	
		],
	/* "pager":"#pager"  */
	}); 
	
	// nable add
	/* jQuery('#tree').jqGrid('navGrid','#pager',
	{
		"edit":true,
		"add":true,
		"del":true,
		"search":false,
		"refresh":true,
		"view":false,
		"excel":false,
		"pdf":false,
		"csv":false,
		"columns":false
	},
	{"drag":true,"resize":true,"closeOnEscape":true,"dataheight":150},
	{"drag":true,"resize":true,"closeOnEscape":true,"dataheight":150}
	); */
	});